package com.sdcqjy.property.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.sdcqjy.mylibrary.widget.ShowToast;
import com.sdcqjy.property.R;
import com.sdcqjy.property.adapter.BookMarkAdapter;
import com.sdcqjy.property.mode.LawRegulationsDetailsMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/7/8.
 * 功能：
 */

public class BookMarkView extends PopupWindow implements BookMarkAdapter.OnAdapterClickListener {
    public BookMarkView(Activity context) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.view_book_mark, null);
        // int h = context.getWindowManager().getDefaultDisplay().getHeight();
        // int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.AnimationPreview);

        initView();
    }

    private void initView() {
        conentView.setOnClickListener(v -> dismiss());
        ButterKnife.bind(this, conentView);
        modeList = new ArrayList<>();
        adapter = new BookMarkAdapter(context, modeList)
                .setOnAdapterClickListener(this);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new LinearLayoutManager(context));
        listView.addItemDecoration(new WItemDecoration(context, 1)
                .setColor(0x99666666));
    }

    public BookMarkView setOnPopupWindowListener(OnPopupWindowListener onPopupWindowListener) {
        this.onPopupWindowListener = onPopupWindowListener;
        return this;
    }

    private Activity context;
    private View conentView;
    private OnPopupWindowListener onPopupWindowListener;
    @BindView(R.id.listView)
    RecyclerView listView;
    private List<LawRegulationsDetailsMode> modeList;
    private BookMarkAdapter adapter;
    private LawRegulationsDetailsMode mode;//当前书签

    public BookMarkView setBookMark(LawRegulationsDetailsMode mode) {
        this.mode = mode;
        return this;
    }

    public BookMarkView setModeList(List<LawRegulationsDetailsMode> list) {
        if (list == null) list = new ArrayList<>();
        modeList.clear();
        modeList.addAll(list);
        adapter.notifyDataSetChanged();
        return this;
    }

    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            super.showAsDropDown(parent);
        } else {
            super.dismiss();
        }
    }

    @OnClick(R.id.textAdd)
    public void onViewClicked() {
        boolean hasMode = false;
        for (int i = 0; i < modeList.size(); i++) {
            LawRegulationsDetailsMode lawRegulationsDetailsMode = modeList.get(i);
            if (lawRegulationsDetailsMode.lawID == mode.id) {
                hasMode = true;
                break;
            }
        }
        if (hasMode) {
            ShowToast.alertShortOfWhite(context, "书签已存在");
        } else {
            if (onPopupWindowListener != null) onPopupWindowListener.addBookMark();
            //dismiss();
        }
    }

    public void addBookMark(LawRegulationsDetailsMode mode) {
        modeList.add(0, mode);
        adapter.notifyDataSetChanged();
    }

    public void removeBookMark(LawRegulationsDetailsMode mode) {
        modeList.remove(mode);
        adapter.notifyDataSetChanged();
    }

    public void deleBookMark(LawRegulationsDetailsMode mode) {
        if (onPopupWindowListener != null) onPopupWindowListener.deleBookMark(mode);
    }

    public void toBookMark(LawRegulationsDetailsMode mode) {
        if (onPopupWindowListener != null) onPopupWindowListener.toBookMark(mode);
        dismiss();
    }

    public interface OnPopupWindowListener {
        void addBookMark();

        void deleBookMark(LawRegulationsDetailsMode mode);

        void toBookMark(LawRegulationsDetailsMode mode);
    }
}
